<?php

namespace app\common\command;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\Children;
use app\admin\model\market\Acceleration;
use app\admin\model\shopro\order\Order;
use app\admin\model\shopro\user\WalletLog;
use app\common\model\shopro\DividendLog;
use app\common\model\shopro\ReleaseAccelerateLogModel;
use app\common\model\shopro\ReleaseAccelerateMarketplaceLogModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\exception\HttpResponseException;

class Jiaquandividend extends Command
{
    protected function configure()
    {
        $this->setName('jiaquandividend')->setHelp('老加权分红--弃用')->setDescription('老加权分红');
    }

    protected function execute(Input $input, Output $output)
    {
        exit();
        $orderModel        = new Order();
        $userModel         = new \app\common\model\User();
        $dividendLogModel  = new DividendLog();
        $info              = $dividendLogModel->where(['dividend_type' => 2])->whereTime('createtime', 'd')->find();
        if(config('site.tiaoshi') == 1){
            $info = '';
        }
        if (!$info) {
            $order_amount = $orderModel->whereNull('activity_type')->whereIn('status', 'paid,completed')->whereTime('createtime',
                'd')->sum('order_amount');
            if ($order_amount) {
                $childrenModel = new Children();
                $userData      = $userModel->with([
                    'acceleration' => function ($query) {
                        $query->field('id,dividend');
                    }
                ])->where(['score' => ['gt', 0]])->whereIn('level', '5,6,7')->field('id,score,level')->select();

                $userData    = collection($userData)->each(function ($query) use ($childrenModel) {
                    $query->children_id = $childrenModel->where(['member_id' => $query->id])->column('children_id');
                });
                $fenhongData = [];
                if ($userData) {
                    Db::startTrans();
                    try {
                        foreach ($userData as $k => $v) {
                            if ($v->children_id) {
                                $userYeji = $orderModel->whereNull('activity_type')->whereIn('user_id',
                                    $v->children_id)->whereIn('status',
                                    'paid,completed')->whereTime('createtime', 'd')->sum('order_amount');
                                if ($userYeji) {
                                    $dividend = $v->acceleration->dividend / 100;
                                    $golden   = bcmul($order_amount, $dividend, 2);
                                    $golden   = bcmul($golden, '0.7', 2);
                                    //对应等级总业绩 / 订单总业绩 * 订单总业绩的70% / 总人数
                                    if (isset($fenhongData[$v->level])) {
                                        $fenhongData[$v->level]['num']     += 1;
                                        $fenhongData[$v->level]['user_id'] .= ','.$v->id;
                                    } else {
                                        $fenhongData[$v->level]['num']     = 1;
                                        $fenhongData[$v->level]['user_id'] = $v->id;
                                        $fenhongData[$v->level]['golden']  = $golden;
                                        $fenhongData[$v->level]['yeji']    = $userYeji;
                                    }
                                }
                            }
                        }
                        if ($fenhongData) {
                            foreach ($fenhongData as $key => $val) {
                                $user_gold = round($val['golden'] / $val['num'], 2);
                                $arr       = explode(',', $val['user_id']);
                                foreach ($arr as $user_id) {
                                    $score = $userModel->where(['id' => $user_id])->value('score');
                                    if ($score <= $user_gold) {
                                        $user_gold = $score;
                                    }
                                    $result = WalletService::change($user_id, 'gold', $user_gold,
                                        'jiaquan_gold',
                                        [],
                                        '分红');
                                    $result = WalletService::change($user_id, 'score', -$user_gold,
                                        'jiaquan_score',
                                        [],
                                        '分红释放');
                                }
                                if ($result) {
                                    $dividendLogModel->create([
                                        'user_ids'      => $val['user_id'],
                                        'user_num'      => $val['num'],
                                        'all_golden'    => $user_gold,
                                        'level'         => $key,
                                        'dividend_type' => 2
                                    ]);
                                    echo '分佣成功';
                                }
                            }
                        }
                        Db::commit();
                    } catch (HttpResponseException $e) {
                        Db::rollback();
                        $data    = $e->getResponse()->getData();
                        $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
                        format_log_error($e, 'Release.release.HttpResponseException', $message);
                    }
                }

            }

        } else {
            echo '今日已分佣';
        }

    }

}
